package me.shurufa.widget.smoothcamera.core;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;


import java.util.Stack;

import me.shurufa.model.AddDigestBean;
import me.shurufa.utils.Utils;
import me.shurufa.widget.smoothcamera.Constants;
import me.shurufa.widget.smoothcamera.Photo;
import me.shurufa.widget.smoothcamera.view.CameraActivity;
import me.shurufa.widget.smoothcamera.view.CropPhotoActivity;

public class CameraManager {

    private static CameraManager mInstance;
    private Stack<Activity> cameras = new Stack<>();

    public static CameraManager getInstance() {
        if (mInstance == null) {
            synchronized (CameraManager.class) {
                if (mInstance == null)
                    mInstance = new CameraManager();
            }
        }
        return mInstance;
    }

    public void openCamera(Activity context) {
        Intent intent = new Intent(context, CameraActivity.class);
        context.startActivityForResult(intent, Constants.REQUEST_CROP);
    }

    public void openCameraWithBookId(Activity context, long bookId) {
        Intent intent = new Intent(context, CameraActivity.class);
        intent.putExtra(me.shurufa.utils.Constants.ARG_BOOK_ID, bookId);
        context.startActivityForResult(intent, Constants.REQUEST_CROP);
    }

    public void openCamera(Activity context, int whereFrom) {
        Intent intent = new Intent(context, CameraActivity.class);
        intent.putExtra(me.shurufa.utils.Constants.ARG_FROM_WHERE, whereFrom);
        context.startActivityForResult(intent, Constants.REQUEST_CROP);
    }

    public void openCamera(Activity context, boolean clearTop) {
        if (!clearTop) {
            this.openCamera(context);
            return;
        }
        Intent intent = new Intent(context, CameraActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivityForResult(intent, Constants.REQUEST_CROP);
    }

    public void openCamera(Activity context, long bookId, AddDigestBean addDigestBean) {
        Intent intent = new Intent(context, CameraActivity.class);
        CameraActivity.initArguments(intent, bookId, addDigestBean);
        context.startActivityForResult(intent, Constants.REQUEST_CROP);
    }

    public void processPhotoItem(Activity activity, Photo photo, AddDigestBean addDigestBean, int whereFrom) {
        Uri uri = photo.getImageUri().startsWith("file:") ? Uri.parse(photo
                .getImageUri()) : Uri.parse("file://" + photo.getImageUri());

        Intent intent = new Intent(activity, CropPhotoActivity.class);
        intent.putExtra(me.shurufa.utils.Constants.ARG_ADD_DIGEST, addDigestBean);
        intent.putExtra(me.shurufa.utils.Constants.ARG_FROM_WHERE, whereFrom);
        intent.setData(uri);
        activity.startActivityForResult(intent, Constants.REQUEST_CROP);
    }

    public void addActivity(Activity act) {
        cameras.add(act);
    }

    public void removeActivity(Activity act) {
        cameras.remove(act);
    }

    public void release() {
        if (!Utils.isNullForList(cameras)) {
            for (Activity activity : cameras) {
                activity.finish();
            }
        }
    }

}
